<?php 
// +----------------------------------------------------------------------
// | @projectName  【order_system---点餐系统】
// +----------------------------------------------------------------------
// | @author        山西创客空间科技有限公司
// +----------------------------------------------------------------------
// | @date          2017年10月13日 星期五
// +----------------------------------------------------------------------
// | @Copyright     http://sx-ck.com All rights reserved.      
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\Common;
use app\common\Base;
use app\common\Error;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Login extends Controller
{
	private $sql = "SELECT a.name,a.password,a.id FROM user a ";

	public function index() 
	{
		return $this -> fetch ();
	}

	public function logincheck() 
	{
		$user = Db::name('user');
		$name = $this -> request -> post('name');
		$password = $this -> request -> post('password');

		if (Common::check_empty($name) === true) {
			return Base::echo_error(Error::USER_NAME_IS_EMPTY);
		}

		if (Common::check_empty($password) === true) {
			return Base::echo_error(Error::PASSWORD_IS_EMPTY);
		}
		$result = $user -> query($this -> sql."where a.name = '$name' and a.password = '$password'");
		if ($result === false) {
			return Base::echo_error(Error::DB_ERROR);
		}

		if (count($result) > 0) {
			$arr = array('name' => $name,'id' => $result[0]['id']);
			Session::set('tmp_login',$arr);
            return Base::echo_success(Error::LOGIN_SUCCESS);
		} else {
			return Base::echo_error(Error::NAME_OR_PASSWORD_ERROR);
		}
	}

	//退出登陆
	public function loginout() 
	{
		// return json_encode('111');
		Session::delete('tmp_login');
		// return 11111;
		if (Common::check_empty(Session::get('tmp_login')) === true) {
            return Base::echo_success(Error::LOGIN_OUT_SUCCESS);
        } else {
        	return Base::echo_error(Error::LOGIN_OUT_ERROR);
        }
	}
}
 ?>
